// pages/roomPattern/roomPattern.js
var util = require('../../utils/util.js');
var app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    userInfo: '', // 登录用户信息
    apartmentList: '', // 公寓集合
    roomList: '', // 房间集合
    floorList: '', // 公寓实际楼层
    hiddenmodalput: true, // 更多条件筛选隐藏状态
    apartmentArray: [], // 公寓信息集合
    apartmentName: '请选择', // 公寓名称
    releaseArray: [{
      "key": "全部",
      "value": ''
    }, {
      "key": "展示中",
      "value": '1'
    }, {
      "key": "隐藏中",
      "value": '0'
    }],
    releaseState: '请选择', // 发布状态
    notImgArray: [{
      "key": "全部",
      "value": '-1'
    }, {
      "key": "有照片",
      "value": '1'
    }, {
      "key": "无照片",
      "value": '0'
    }],
    notImg: '请选择', // 有无照片
    leaseState: '-1', // 出租状态
    apartment_id: '', // 公寓ID
    keyword: '', // 关键字
    room_show: '', // 发布状态
    imgState: '-1', // 有无图片
    hiddendata: true, // 有无数据
    limit: 3, // 每页显示记录数
    pageIndex: 1, // 当前页
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    var that = this;
    wx.getStorage({
      key: 'userInfo',
      success: function(res) {
        var data = res.data;
        that.setData({
          userInfo: data
        });
        if (data.user_type_id == 10006) {
          app.editTabBar2();
        }
        that.getRoomData();
      },
      fail: function() {
        wx.redirectTo({
          url: '/pages/index/index',
        })
      }
    });
  },

  /**
   * 获取公寓房间数据
   */
  getRoomData: function() {
    var that = this;
    var data = that.data;
    var user = data.userInfo;
    var userId = user.user_id; // 登录人ID
    var source = user.source; // 房东ID
    var userTypeId = user.user_type_id; // 登录人用户类型
    var leaseState = data.leaseState; // 出租状态
    var apartment_id = data.apartment_id; // 公寓ID
    var keyword = data.keyword; // 关键字
    var room_show = data.room_show; // 发布状态
    var imgState = data.imgState; // 有无图片
    var pageIndex = data.pageIndex;
    var limit = data.limit;
    wx.showLoading({
      title: '加载中···'
    })
    util.getReq('/app/home/getRoomPattern', {
      userTypeId: userTypeId,
      'term.state': leaseState,
      'term.apartment_id': apartment_id,
      'term.keyword': keyword,
      'term.room_show': room_show,
      'term.notImg': imgState,
      "term.pageIndex": pageIndex,
      "term.limit": limit
    }, function(data) {
      if (data.code == 0) {
        var data = data.params;
        var pagedList = data.pagedList;
        if (limit >= pagedList.recordCount) {
          that.setData({
            hiddendata: false
          });
        } else {
          that.setData({
            hiddendata: true,
          });
        }
        that.setData({
          roomList: data.roomList,
          floorList: data.floorList,
          apartmentList: pagedList.data,
          recordCount: pagedList.recordCount
        });
      } else {
        util.isError(data.desc);
      }
      wx.hideLoading();
    });
  },

  /**
   * 出租状态切换
   */
  tab: function(e) {
    var that = this;
    var dataId = e.currentTarget.id;
    that.setData({
      leaseState: dataId
    });
    that.getRoomData();
  },

  /**
   * 显示更多条件筛选
   */
  onMore: function() {
    var that = this;
    that.setData({
      hiddenmodalput: false
    });
    wx.getStorage({
      key: 'userInfo',
      success: function(res) {
        var data = res.data;
        that.getApartment(data);
      },
      fail: function() {
        wx.redirectTo({
          url: '/pages/index/index',
        })
      }
    });
  },

  /**
   * 确定
   */
  onConfirm: function() {
    var that = this;
    that.setData({
      hiddenmodalput: true
    });
    that.getRoomData();
  },

  /**
   * 取消
   */
  onCancel: function() {
    var that = this;
    that.setData({
      hiddenmodalput: true
    });
  },

  /**
   * 获取公寓信息集合
   */
  getApartment: function(data) {
    var that = this;
    if (that.data.apartmentArray.length == 0) {
      util.getReq("/app/home/allApartmentInterface", {
        source: data.source,
        uid: data.user_id
      }, function(datas) {
        if (datas.code == 0) {
          var list = datas.params.list;
          var length = list.length;
          var listArray = [{
            'key': '全部',
            'value': '-1'
          }];
          for (var i = 0; i < length; i++) {
            var ob = new Object();
            ob.key = list[i].apartment_name;
            ob.value = list[i].apartment_id;
            listArray.push(ob);
          }
          that.setData({
            apartmentArray: listArray
          })
        } else {
          util.isError(data.desc);
        }
      })
    }
  },

  /**
   * 公寓选择
   */
  bindApartmentChange: function(e) {
    this.setData({
      apartment_id: this.data.apartmentArray[e.detail.value].value,
      apartmentName: this.data.apartmentArray[e.detail.value].key
    })
  },

  /**
   * 发布状态选择
   */
  bindReleaseChange: function(e) {
    this.setData({
      room_show: this.data.releaseArray[e.detail.value].value,
      releaseState: this.data.releaseArray[e.detail.value].key
    })
  },

  /**
   * 有无图片选择
   */
  bindNotImgChange: function(e) {
    this.setData({
      imgState: this.data.notImgArray[e.detail.value].value,
      notImg: this.data.notImgArray[e.detail.value].key
    })
  },

  /**
   * 关键字输入框
   */
  onKeywordInput: function(e) {
    this.setData({
      keyword: e.detail.value
    })
  },

  /**
   * 房间详情跳转
   */
  onRoomDetail: function(e) {
    var roomId = e.currentTarget.id;
    var oid = e.currentTarget.dataset.oid;
    wx.navigateTo({
      url: 'roomDetail?id=' + roomId + '&oid=' + oid
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {
    this.onLoad();
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {
    var that = this;
    var data = that.data;
    var limit = data.limit + 1;
    if (data.limit < data.recordCount) {
      limit = data.limit + 1;
      that.setData({
        limit: limit
      });
      that.getRoomData();
    }
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function() {

  }

})